package zhongbo.java.air.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.model.FlightActivityDaily;
import zhongbo.java.air.service.IFlightActivityDailyService;
import zhongbo.java.air.task.AircraftActivityTask;

import java.util.Date;
import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;
    @Autowired
    private AircraftActivityTask aircraftActivityTask;
    @Override
    public FlightActivityDaily add(FlightActivityDaily flightActivityDaily) {
        ValidFlightActivity(flightActivityDaily);
        flightActivityDaily.setCreatedTime(new Date());
        flightActivityDaily.setUpdatedTime(new Date());
        int insert = flightActivityDailyMapper.insert(flightActivityDaily);
        if (insert > 0) {
            return flightActivityDaily;
        }
        throw new RuntimeException("插入失败");
    }

    private void ValidFlightActivity(FlightActivityDaily flightActivityDaily) {
        String Registration = flightActivityDaily.getRegistration();
        if (StrUtil.isBlank(Registration)) {
            throw new RuntimeException("注册编码不能为空");
        }
        String Icao = flightActivityDaily.getIcaoCode();
        if (StrUtil.isBlank(Icao)) {
            throw new RuntimeException("Icao编码不能为空");
        }
        List<FlightActivityDaily> valid = flightActivityDailyMapper.selectList(null);
        for (FlightActivityDaily activityDaily : valid) {
            if (activityDaily.getIcaoCode().equals(flightActivityDaily.getIcaoCode())) {
                throw new RuntimeException("Icao是唯一的，已经使用过了");
            }

        }
    }

    @Override
    public String delete(String id) {
        if (StrUtil.isBlank(id)){
            throw new RuntimeException("id为空");
        }
        int delete=flightActivityDailyMapper.deleteById(id);
        if (delete>0){
            return id;
        }
        throw new RuntimeException("删除失败");
    }

    @Override
    public FlightActivityDaily edit(FlightActivityDaily edit) {
        ValidFlightActivity(edit);
        edit.setUpdatedTime(new Date());
        int editSelect=flightActivityDailyMapper.updateById(edit);
        if (editSelect>0){
            return edit;
        }
        throw new RuntimeException("修改失败");
    }

    @Override
    public FlightActivityDaily selectId(String id) {
        if (StrUtil.isBlank(id)){
            throw new RuntimeException("id为空");
        }
        return flightActivityDailyMapper.selectById(id);
    }

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {

        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
        }
        // todo 根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回
       QueryWrapper<FlightActivityDaily>queryWrapper=new QueryWrapper<>();
        if (StrUtil.isNotBlank(date)){
            queryWrapper.ge("date",date);
        }
        List<FlightActivityDaily> status=flightActivityDailyMapper.selectList(queryWrapper);
            if (status!=null){
                return status;
            }
        throw new RuntimeException("今天没有飞机活动");
    }

    @Override
    public PageVo<FlightActivityDaily> getDailyActivityList(Integer page, Integer pageSize
            , String icao, String startDate, String endDate
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightActivityDaily> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)){
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field),keyword);
            }
        }
        // 根据日期查询
        queryWrapper.eq("date", startDate);

        //  ICAO 码进行精准查询
        if (StrUtil.isNotBlank(icao)) {
            queryWrapper.eq("icao_code", icao);
        }

        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(oderByField);
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(oderByField);
            }
        }
        Page<FlightActivityDaily> pageParam = new Page<>(page, pageSize);
        Page<FlightActivityDaily> userPage = flightActivityDailyMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(userPage);
    }
    }
